import pygame
from pygame.sprite import Sprite


class Ship(Sprite):
    def __init__(self, ai_setting, screen):
        super(Ship, self).__init__();
        self.screen = screen;
        self.ai_setting = ai_setting;
        self.image = pygame.image.load("images/ship.bmp")
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        self.moving_right = False;
        self.moving_left = False;
        self.moving_up = False;
        self.moving_down = False;
        self.centerx = (float)(self.rect.centerx)
        self.centery = (float)(self.rect.centery)
        self.height = self.rect.height;

    def draw(self):
        self.screen.blit(self.image, self.rect)

    def updatePosition(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.centerx += self.ai_setting.ship_speed_factor;
        # 这里不用elif的理由是：用户可能同时按下左右键
        if self.moving_left and self.rect.left > 0:
            self.centerx -= self.ai_setting.ship_speed_factor;
        self.rect.centerx = self.centerx;

        if self.moving_up and self.rect.top > 0:
            self.centery -= self.ai_setting.ship_speed_factor;
        # 这里不用elif的理由是：用户可能同时按下左右键
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.centery += self.ai_setting.ship_speed_factor;
        self.rect.centery = self.centery;

    def center_ship(self):
        self.centerx = self.screen_rect.centerx;
        self.centery = self.screen_rect.bottom - self.height;
